using System.Reflection;
using NUnit.Framework;
using ContentRenderingApi;
using ContentRenderingApiSystemTest.Helper;
using System.IO;
using System.Text;
using System;

namespace ContentRenderingApiSystemTest.TestFixture
{
    [TestFixture]
    public class RenderLoggerTestFixture
    {
        [Test]
        public void LoggerTest()
        {
            AbstractTemplate template = new EmbeddedResourceTemplate(
                Assembly.GetExecutingAssembly(),
                "ContentRenderingApiSystemTest.TestFixture.Input",
                "Logger");
         
            StringBuilder stringBuilder = new StringBuilder();   

            template.ApplyLogger((level,unparsedLines) => stringBuilder.AppendLine(RenderLoggerTestFixture.Logger(level, unparsedLines)));

            // don't care about the output
            template.Render();

            Assert.AreEqual(
                ResourceHelper.FetchString("ContentRenderingApiSystemTest.TestFixture.Output.Logger.txt"),
                stringBuilder.ToString());
        }

        private static string Logger(int level, string unparsedLines)
        {
            // split into lines
            string[] lines = unparsedLines.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            StringBuilder stringBuilder = new StringBuilder();

            foreach (string line in lines)
            {
                // write the idented line
                stringBuilder.Append(new string('>', level * 2));
                stringBuilder.Append(line);
            }

            return stringBuilder.ToString();
        }
    }
}
